package com.yumeng.framework.persistence.mybatis.plugins.dynamictable.parser;


import com.yumeng.framework.persistence.mybatis.plugins.dynamictable.DynamicTableNameMode;

import java.util.Map;

/**
 * 按id取模表名处理器
 * tableMap:表名->分几张表
 *
 * @author wxd
 * @date 2022/9/1 10:13
 */
public class IdModTableNameParser extends AbstractTableNameMapParser<Long, Integer> {

    public IdModTableNameParser(Map<String, Integer> tableMap) {
        super(tableMap, DynamicTableNameMode.ID_MOD);
    }

    @Override
    protected String doDynamicTableName(String sql, String tableName, Long param) {
        //分表个数
        Integer tableCount = getTableMapVal(tableName);
        // param 当前要分表的id
        // 当前id的数据应该保存在第几张表
        int tableIndex = (int) (param % tableCount);

        return tableName + "_" + tableIndex;
    }
}
